home *** CD-ROM | disk | FTP | other *** search
Text File | 1988-06-01 | 1.8 KB | 52 lines | [TEXT/CCL ] |
- ; © Copyright 1988 Jean-Pascal J. LANGE.
-
- ; towerOfHanoi, first pole is 0
-
- (defStruct HanoiTower (stacks nil))
-
- (deFun Hanoi (tower)
- ; tower of Hanoï program. Asks user for height of stack of disks.
- ; stacks is an array of stacks. Each stack is a list.
- ; The "objects" we put on the stacks are characters.
- ; A is the smallest disk, B is larger, etc...
- (let ((height nil))
- (do ()
- ((integerP height))
- (format t "~&Please type the number of disks in the tower: ")
- (setq height (read)) )
- (format t "~&tower of Hanoï for ~D disk~:P." height)
- (setf (HanoiTower-stacks tower)
- (make-array 3 :initial-element nil) )
-
- (do ((each height (1- each)))
- ((zerop each))
- (addFirst (HanoiTower-stacks tower) 0
- (code-char (+ (char-code #\A) (1- each))) ) )
- (moveTower tower height 1 3 2) ) )
-
- (deFun moveTower (tower nDisks fromPin toPin usingPin)
- (cond ((> nDisks 0)
- (moveTower tower (1- nDisks) fromPin usingPin toPin)
- (moveDisk tower fromPin toPin)
- (moveTower tower (1- nDisks) usingPin toPin fromPin) ) ) )
-
- (deFun moveDisk (tower fromPin toPin)
- ; moves disk from a pin to another pin. Print the results in the
- ; listener window.
- (let ((disk (getAndRemoveFirst (HanoiTower-stacks tower)
- (1- fromPin) )))
- (addFirst (HanoiTower-stacks tower) (1- toPin) disk)
- (format t "~&~D -> ~D ~A" fromPin toPin disk) ) )
-
- (deFun addFirst (array index item)
- ; addFirst is the procedure for push.
- (setf (aref array index)
- (cons item (aref array index)) ) )
-
- (deFun getAndRemoveFirst (array index)
- ; getAndRemoveFirst is the procedure for pop.
- (let ((first (car (aref array index))))
- (setf (aref array index)
- (cdr (aref array index)) )
- first ) )
-